package com.langsin.guochunrui.mapper;


import com.langsin.bean.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Mytest {

    SqlSession sqlSession=null;

    @Test
    public void testUseGoOdsLazy() throws Exception {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserGoods(702);
        System.out.println(user.getCname() + "," + user.getAddress());

        try {
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (Orders orders : user.getOrdersList()) {
            System.out.println(orders.getOrderId());

            try{Thread.sleep(3000);}catch(Exception e){e.printStackTrace();}

            for (OrderDetail orderDetail : orders.getOrderDetailList()) {
                System.out.println(orderDetail.getDetailId());

                try{Thread.sleep(2000);}catch(Exception e){e.printStackTrace();}
                System.out.println(orderDetail.getGoods());
            }
        }



    }


    //懒加载测试1
    @Test
    public void testLazy1() throws Exception{
        OrdersMapper ordersMapper =sqlSession.getMapper(OrdersMapper.class);
        Orders orders = ordersMapper.queryOrdersAndUserByOrdersId(1001);

        System.out.println(orders.getPrice()+"~~"+orders.getOrderDate());

        //休息10s
        try {Thread.sleep(10000); }catch (Exception exception){ exception.printStackTrace();}
        //休息完之后
        System.out.println(orders.getUser().toString());

    }

    //多对多
    @Test
    public void testUseGoOds() throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserGoods(704);
        System.out.println(user);
        System.out.println("--------------------");
        for (Orders orders : user.getOrdersList()) {
            for (OrderDetail orderDetail : orders.getOrderDetailList()) {
                System.out.println(orderDetail.getGoods());
            }
        }
    }


    //一对一查询 原理相似与一对多
    //查询清单的同时查询用户信息
    @Test
    public void queryOrdersAndUserByOrdersId(){
        OrdersMapper ordersMapper =sqlSession.getMapper(OrdersMapper.class);
        Orders orders = ordersMapper.queryOrdersAndUserByOrdersId(1003);
        System.out.println(orders.toString());

    }


    //根据用户id更新用户属性
    @Test
    public void testUpdateWorkerByWid(){
        WorkerMapper workerMapper=sqlSession.getMapper(WorkerMapper.class);

        Worker worker=new Worker();
        worker.setWid("d5dd382c-f4f2-11ec-b0b0-00ff20897877");
        worker.setWname("小白兔");
        worker.setAge(16);
        worker.setSex("女");
        int i = workerMapper.updateWorkerByWid(worker);
        System.out.println(i);
    }

    //查询用户订单
    //一（用户）对多（订单）
    @Test
    public void testQueryUserOrders() throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserOrders(702);
        System.out.println(user.toString());

//        for (Orders orders : user.getOrdersList()) {
//            System.out.println(orders+"!!!");
//        }

    }

    @Test
    public void testUserManyQuery(){

        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        Worker worker=new Worker();

        worker.setWname("%孙%");
        worker.setAge(19);
        worker.setSex("男");

        List<Worker> workers = workerMapper.workQueryByCondition(worker);

        workers.forEach(System.out::println);

    }


    @Test
    public void testManyQuery(){

        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        Goods goods=new Goods();
        goods.setGoodsName("%鼠%");
        goods.setShopPrice(100.00);

        List<Goods> goods1 = goodsMapper.queryByCondition(goods);
        goods1.forEach(System.out::println);


    }

    @Test
    public void testListById(){
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Integer> list=new ArrayList<>();

        list.add(9001);
        list.add(9002);
        list.add(9003);
        list.add(9005);

        List<Goods> goods = goodsMapper.queryByIds(list);
        goods.forEach(System.out::println);


    }




    //每次单元测试类之前执行
    @Before
    public void before() throws Exception{
        //读取配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        //工厂
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        //获取一个会话（连接）默认不提交
        sqlSession = sqlSessionFactory.openSession();
    }
    //每次单元测试类之后执行
    @After
    public void after() throws Exception{
        sqlSession.commit();
        sqlSession.close();
    }
}
